package com.ronsicecream.pos.master.item;

import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Sequence;
import com.buanalintas.bynga.entities.MasterUnit;
import com.ronsicecream.pos.entities.PosMasterItem;
import com.ronsicecream.pos.entities.PosMasterItemStock;
import com.ronsicecream.pos.entities.PosMasterRegional;
import com.ronsicecream.pos.entities.PosMasterUnit;
import java.math.BigDecimal;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class Item extends Page {

    private Field itemId;
    private Field itemCode;
    private Field itemName;
    private Field itemMinimum;
    private Field itemPriceBuy;
    private Field itemPriceSell;
    private Field itemPriceOutlet;
    private Field itemPriceMainKitchen;
    private Field itemDescription;
    private Field itemSatuanId;
    private Field itemSatuanOrderId;
    private Field itemTypeId;
    private Field itemRasaId;

    public Item() {
        setTheClass(PosMasterItem.class);

        itemId = new Field();
        itemId.setName("itemId");
        itemId.setCaption("id");
        itemId.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        itemId.setInputProperties(" size=\"20\"");
        getFields().add(itemId);

        itemCode = new Field();
        itemCode.setName("itemCode");
        itemCode.setCaption("code Item");
        itemCode.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemCode.setInputProperties(" size=\"10\"");
        itemCode.setRequired(true);
        itemCode.setInputCase(Field.CASE_UPPER);
        getFields().add(itemCode);

        itemName = new Field();
        itemName.setName("itemName");
        itemName.setCaption("Name");
        itemName.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemName.setInputProperties(" size=\"30\"");
        itemName.setRequired(true);
        itemName.setInputCase(Field.CASE_UPPER);
        getFields().add(itemName);

//        itemPriceBuy = new Field();
//        itemPriceBuy.setName("itemPriceBuy");
//        itemPriceBuy.setCaption("price buy");
//        itemPriceBuy.setListProperties(" align=\"center\" width=\"15%\" nowrap");
//        itemPriceBuy.setInputProperties(" size=\"20\"");
//        itemPriceBuy.setRequired(true);
//        itemPriceBuy.setDefaultValue(0);
//        getFields().add(itemPriceBuy);
//        
//        itemPriceSell = new Field();
//        itemPriceSell.setName("itemPriceSell");
//        itemPriceSell.setCaption("price sell");
//        itemPriceSell.setListProperties(" align=\"center\" width=\"15%\" nowrap");
//        itemPriceSell.setInputProperties(" size=\"20\"");
//        itemPriceSell.setRequired(true);
//        itemPriceSell.setDefaultValue(0);
//        getFields().add(itemPriceSell);
        
//        itemPriceMainKitchen = new Field();
//        itemPriceMainKitchen.setName("itemPriceMainKitchen");
//        itemPriceMainKitchen.setCaption("price main kitchen");
//        itemPriceMainKitchen.setListProperties(" align=\"center\" width=\"15%\" nowrap");
//        itemPriceMainKitchen.setInputProperties(" size=\"20\"");
//        itemPriceMainKitchen.setRequired(true);
//        itemPriceMainKitchen.setDefaultValue(0);
//        getFields().add(itemPriceMainKitchen);
//        
//        itemPriceOutlet = new Field();
//        itemPriceOutlet.setName("itemPriceOutlet");
//        itemPriceOutlet.setCaption("price outlet");
//        itemPriceOutlet.setListProperties(" align=\"center\" width=\"15%\" nowrap");
//        itemPriceOutlet.setInputProperties(" size=\"20\"");
//        itemPriceOutlet.setRequired(true);
//        itemPriceOutlet.setDefaultValue(0);
//        getFields().add(itemPriceOutlet);
//        
        
        itemTypeId = new Field();
        itemTypeId.setName("itemTypeId");
        itemTypeId.setCaption("type");
        itemTypeId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemTypeId.setInputProperties(" size=\"30\"");
        itemTypeId.setRequired(true);
        itemTypeId.setInputCase(Field.CASE_UPPER);
        getFields().add(itemTypeId);
        
//        itemRasaId = new Field();
//        itemRasaId.setName("itemRasaId");
//        itemRasaId.setCaption("rasa");
//        itemRasaId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
//        itemRasaId.setInputProperties(" size=\"30\"");
//        itemRasaId.setRequired(true);
//        itemRasaId.setInputCase(Field.CASE_UPPER);
//        getFields().add(itemRasaId);
        
        itemMinimum = new Field();
        itemMinimum.setName("itemMinimum");
        itemMinimum.setCaption("Item Minimum");
        itemMinimum.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemMinimum.setInputProperties(" size=\"10\" style=\"text-align: right;\"");
//        itemMinimum.setInputCase(Field.CASE_UPPER);
        itemMinimum.setRequired(true);
        itemMinimum.setDefaultValue(1.0);
        getFields().add(itemMinimum);
        
        itemSatuanId = new Field();
        itemSatuanId.setName("itemSatuanId");
        itemSatuanId.setCaption("satuan");
        itemSatuanId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemSatuanId.setInputProperties(" size=\"30\"");
        itemSatuanId.setRequired(true);
        itemSatuanId.setInputCase(Field.CASE_UPPER);
        getFields().add(itemSatuanId);
        
        itemSatuanOrderId = new Field();
        itemSatuanOrderId.setName("itemSatuanOrderId");
        itemSatuanOrderId.setCaption("satuan order");
        itemSatuanOrderId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        itemSatuanOrderId.setInputProperties(" size=\"30\"");
        itemSatuanOrderId.setRequired(true);
        itemSatuanOrderId.setInputCase(Field.CASE_UPPER);
        getFields().add(itemSatuanOrderId);
        
        itemDescription = new Field();
        itemDescription = new Field();
        itemDescription.setName("itemDescription");
        itemDescription.setCaption("description");
        itemDescription.setListProperties(" width=\"30%\"");
        itemDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        itemDescription.setInputType(Field.INPUT_TEXT_AREA);
        itemDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(itemDescription);
        

        fillFields();

        getFieldsUnique().add(new Field[]{itemCode});

        getFieldsAdd().remove(itemId);
        getFieldsAdd().remove(itemRasaId);
        getFieldsAdd().remove(itemPriceBuy);
        getFieldsAdd().remove(itemPriceMainKitchen);
        getFieldsAdd().remove(itemPriceOutlet);
        getFieldsAdd().remove(itemPriceSell);

        getFieldsEdit().remove(itemId);
         getFieldsEdit().remove(itemRasaId);
        getFieldsEdit().remove(itemPriceBuy);
        getFieldsEdit().remove(itemPriceMainKitchen);
        getFieldsEdit().remove(itemPriceOutlet);
        getFieldsEdit().remove(itemPriceSell);
        
        getFieldsList().remove(itemId);
         getFieldsList().remove(itemRasaId);
        getFieldsList().remove(itemPriceBuy);
        getFieldsList().remove(itemPriceMainKitchen);
        getFieldsList().remove(itemPriceOutlet);
        getFieldsList().remove(itemPriceSell);
        
        getFieldsPrint().remove(itemId);
        getFieldsPrint().remove(itemRasaId);
        getFieldsPrint().remove(itemPriceBuy);
        getFieldsPrint().remove(itemPriceMainKitchen);
        getFieldsPrint().remove(itemPriceOutlet);
        getFieldsPrint().remove(itemPriceSell);
        
        getFieldsPrintList().remove(itemId);
        getFieldsPrintList().remove(itemRasaId);
        getFieldsPrintList().remove(itemPriceBuy);
        getFieldsPrintList().remove(itemPriceMainKitchen);
        getFieldsPrintList().remove(itemPriceOutlet);
        getFieldsPrintList().remove(itemPriceSell);
        
        getFieldsView().remove(itemId);
        getFieldsView().remove(itemRasaId);
        getFieldsView().remove(itemPriceBuy);
        getFieldsView().remove(itemPriceMainKitchen);
        getFieldsView().remove(itemPriceOutlet);
        getFieldsView().remove(itemPriceSell);
        
        getFieldsSearch().remove(itemId);
        setExportList(getClass().getResourceAsStream("ItemList.jasper"));
        setExport(getClass().getResourceAsStream("Item.jasper"));
        getController().setOrder(itemId);
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Item"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }

    public static PosMasterItemStock getStockItem(EntityManager em, PosMasterItem item, MasterUnit unit) {
        try {
            Query query = em.createQuery("SELECT t FROM PosMasterItemStock t WHERE "
                    + " t.itemStockItemId=:itemStockItemId "
                    + " AND t.itemStockUnitId=:itemStockUnitId ");
//                    + " AND t.itemStockPosUnitId=:itemStockPosUnitId"
//                    + " AND t.itemStockRegionalId=:itemStockRegionalId");
            query.setParameter("itemStockItemId", item);
            query.setParameter("itemStockUnitId", unit);
//            query.setParameter("itemStockPosUnitId", posunit);
//            query.setParameter("itemStockRegionalId", regional);
            return (PosMasterItemStock) query.getSingleResult();
        } catch (Exception e) {
            Logger.getInstance();
        }
        PosMasterItemStock stock = new PosMasterItemStock();
        stock.setItemStockId(Sequence.next(stock));
        stock.setItemStockItemId(item);
        stock.setItemStockUnitId(unit);
//        stock.setItemStockPosUnitId(posunit);
//        stock.setItemStockRegionalId(regional);
        stock.setItemStockValue(BigDecimal.ZERO);
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
            em.persist(stock);
            em.getTransaction().commit();
        } else {
            em.persist(stock);
        }
        return stock;
    }
    
    public static PosMasterItemStock getStockWarehouse(EntityManager em, PosMasterItem item, MasterUnit unit, PosMasterUnit posunit, PosMasterRegional regional) {
        try {
            Query query = em.createQuery("SELECT t FROM PosMasterItemStock t WHERE "
                    + " t.itemStockItemId=:itemStockItemId "
                    + " AND t.itemStockUnitId=:itemStockUnitId "
                    + " AND t.itemStockPosUnitId=:itemStockPosUnitId"
                    + " AND t.itemStockRegionalId=:itemStockRegionalId");
            query.setParameter("itemStockItemId", item);
            query.setParameter("itemStockUnitId", unit);
            query.setParameter("itemStockPosUnitId", posunit);
            query.setParameter("itemStockRegionalId", regional);
            return (PosMasterItemStock) query.getSingleResult();
        } catch (Exception e) {
            Logger.getInstance();
        }
        PosMasterItemStock stock = new PosMasterItemStock();
        stock.setItemStockId(Sequence.next(stock));
        stock.setItemStockItemId(item);
        stock.setItemStockUnitId(unit);
        stock.setItemStockPosUnitId(posunit);
        stock.setItemStockRegionalId(regional);
        stock.setItemStockValue(BigDecimal.ZERO);
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
            em.persist(stock);
            em.getTransaction().commit();
        } else {
            em.persist(stock);
        }
        return stock;
    }
    
    public static PosMasterItemStock getStockMainkitchen(EntityManager em, PosMasterItem item, MasterUnit unit, PosMasterUnit posunit, PosMasterRegional regional) {
        try {
            Query query = em.createQuery("SELECT t FROM PosMasterItemStock t WHERE "
                    + " t.itemStockItemId=:itemStockItemId "
                    + " AND t.itemStockUnitId=:itemStockUnitId "
                    + " AND t.itemStockPosUnitId=:itemStockPosUnitId"
                    + " AND t.itemStockRegionalId=:itemStockRegionalId");
            query.setParameter("itemStockItemId", item);
            query.setParameter("itemStockUnitId", unit);
            query.setParameter("itemStockPosUnitId", posunit);
            query.setParameter("itemStockRegionalId", regional);
            return (PosMasterItemStock) query.getSingleResult();
        } catch (Exception e) {
            Logger.getInstance();
        }
        PosMasterItemStock stock = new PosMasterItemStock();
        stock.setItemStockId(Sequence.next(stock));
        stock.setItemStockItemId(item);
        stock.setItemStockUnitId(unit);
        stock.setItemStockPosUnitId(posunit);
        stock.setItemStockRegionalId(regional);
        stock.setItemStockValue(BigDecimal.ZERO);
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
            em.persist(stock);
            em.getTransaction().commit();
        } else {
            em.persist(stock);
        }
        return stock;
    }
    
    public static PosMasterItemStock getStockOutlet(EntityManager em, PosMasterItem item, MasterUnit unit, PosMasterUnit posunit, PosMasterRegional regional) {
        try {
            Query query = em.createQuery("SELECT t FROM PosMasterItemStock t WHERE "
                    + " t.itemStockItemId=:itemStockItemId "
                    + " AND t.itemStockUnitId=:itemStockUnitId "
                    + " AND t.itemStockPosUnitId=:itemStockPosUnitId"
                    + " AND t.itemStockRegionalId=:itemStockRegionalId");
            query.setParameter("itemStockItemId", item);
            query.setParameter("itemStockUnitId", unit);
            query.setParameter("itemStockPosUnitId", posunit);
            query.setParameter("itemStockRegionalId", regional);
            return (PosMasterItemStock) query.getSingleResult();
        } catch (Exception e) {
            Logger.getInstance();
        }
        PosMasterItemStock stock = new PosMasterItemStock();
        stock.setItemStockId(Sequence.next(stock));
        stock.setItemStockItemId(item);
        stock.setItemStockUnitId(unit);
        stock.setItemStockPosUnitId(posunit);
        stock.setItemStockRegionalId(regional);
        stock.setItemStockValue(BigDecimal.ZERO);
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
            em.persist(stock);
            em.getTransaction().commit();
        } else {
            em.persist(stock);
        }
        return stock;
    }
}